package ReflectClassDemo;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

class Student {

    //私有属性name
    private String name = "bit";
    //公有属性age
    public int age = 18;

    //不带参数的构造方法
    public Student() {
        System.out.println("Student()");
    }

    private Student(String name, int age) {
        this.name = name;
        this.age = age;
        System.out.println("Student(String,name)");
    }

    private void eat() {
        System.out.println("i am eat");
    }

    public void sleep() {
        System.out.println("i am pig");
    }

    private void function(String str) {
        System.out.println(str);
    }

    @Override
    public String toString() {
        return "Student{" + "name='" + name + '\'' + ", age=" + age + '}';
    }
}
public class Main {
    public static void reflectPrivateMethod() {
        Class<?> c3 = null;

        try {
            c3 = Class.forName("ReflectClassDemo.Student");
            Student student = (Student) c3.newInstance();

            Method method = c3.getDeclaredMethod("function", String.class);
            method.setAccessible(true);
            method.invoke(student,"温习文");
            System.out.println(student);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
    public static void  reflectPrivateField() {
        Class<?> c1= null;

        try {
            c1 = Class.forName("ReflectClassDemo.Student");
            Student student = (Student)c1.newInstance();

            Field field = c1.getDeclaredField("name");
            field.setAccessible(true);
            field.set(student,"cao");

            System.out.println(student);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

    }
    public static void reflectClassDemo() {
        Class<?> c2 = null;

        try {
            c2 =  Class.forName("ReflectClassDemo.Student");

            Constructor<?> constructor = c2.getDeclaredConstructor(String.class,int.class);
            //通过Class对象创建java文件里的实例对象
            Student student = (Student) c2.newInstance();
            constructor.setAccessible(true);
            //通过Class对象调用构造
            Student student1 = (Student) constructor.newInstance("纹细纹",18);
            System.out.println(student1);



        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

    }
    public static void main(String[] args) {
       reflectClassDemo();
        reflectPrivateField();
        reflectPrivateMethod();
    }
}
